/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.model.persistence;

import com.agenda.model.entity.AmTbAgenMedi;
import com.agenda.model.repository.AgendaRepository;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Ronildo
 */
public class AgendaDAO  extends GenericDAO<AmTbAgenMedi, AgendaRepository> implements AgendaRepository {
    
    public AmTbAgenMedi findById(Long id){
        return (AmTbAgenMedi)em.createNamedQuery("AmTbAgenMedi.findByNrSequAgenMedi").
                setParameter("nrSequAgenMedi", id).getSingleResult();
    }

    public List<AmTbAgenMedi> list(AmTbAgenMedi agenda){
        try{
            String hql = "select agenda from AmTbAgenMedi as agenda ";
            hql+=" where agenda.nrSequAgenMedi = agenda.nrSequAgenMedi";
            
            if(agenda.getNrSequAgenMedi() > 0)
                    hql+=" and agenda.nrSequAgenMedi = :nrSequAgenMedi";
            if(agenda.getDtRefe() != null )
                    hql+=" and agenda.dtRefe = :dtRefe";
            if(agenda.getHrInicCmpr() > 0)
                    hql+=" and agenda.hrInicCmpr = :hrInicCmpr";
            if(agenda.getHrTermCmpr() > 0)
                    hql+=" and agenda.hrTermCmpr = :hrTermCmpr";
            if(agenda.getDtUltiAtua() !=null)
                hql+=" and agenda.dtUltiAtua = :dtUltiAtua";
            hql+=" order by agenda.nrSequAgenMedi";

            Query query = em.createQuery(hql);
            if(agenda.getNrSequAgenMedi() > 0)
                    query.setParameter("nrSequAgenMedi", agenda.getNrSequAgenMedi());
            if(agenda.getDtRefe() != null )
                    query.setParameter("dtRefe", agenda.getDtRefe());
            if(agenda.getHrInicCmpr() > 0)
                    query.setParameter("hrInicCmpr", agenda.getHrInicCmpr());
            if(agenda.getHrTermCmpr() > 0)
                    query.setParameter("hrTermCmpr", agenda.getHrTermCmpr());
            if(agenda.getDtUltiAtua() !=null)
                    query.setParameter("dtUltiAtua", agenda.getDtUltiAtua());
            
            return query.getResultList();
        }catch(RuntimeException e){
            throw new DAOException(e);
        }
    }

    public List<AmTbAgenMedi> listAll(){
        try{
            return em.createNamedQuery("AmTbAgenMedi.findAll").getResultList();
        }catch(IllegalStateException e){
            throw new DAOException(e);
        }
    }
}